package common;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.hichip.campro.R;

public class TitleView extends RelativeLayout implements OnClickListener {
    public static final int NAVIGATION_BUTTON_LEFT = 0;
    public static final int NAVIGATION_BUTTON_RIGHT = 1;
    public static final int NAVIGATION_BUTTON_IV_RIGHT = 2;
    private NavigationBarButtonListener btnListener;

    public TitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.title_view_in_all_activity_view, this, true);
    }

    public void setTitle(String txt) {
        TextView tv =findViewById(R.id.title_middle);
        tv.setVisibility(View.VISIBLE);
        tv.setText(txt);
    }

    public  void setRightTxt(String string){
        TextView tv_right = findViewById(R.id.tv_right);
        tv_right.setText(string);
    }

    public  void setRightTxtBack(int  resId){
        ImageView iv_right = findViewById(R.id.iv_right);
        iv_right.setVisibility(View.VISIBLE);
        iv_right.setTag(NAVIGATION_BUTTON_IV_RIGHT);
        iv_right.setOnClickListener(this);
        iv_right.setImageResource(resId);
    }

    public void setRightTxtGone(){
        TextView tv_right = findViewById(R.id.tv_right);
        tv_right.setVisibility(View.GONE);
    }

    public void setButton(int which) {
        switch (which) {
            case NAVIGATION_BUTTON_LEFT: {
                ImageView btn =findViewById(R.id.btn_return);
                btn.setTag(which);
                btn.setVisibility(View.VISIBLE);
                btn.setOnClickListener(this);
            }
            break;
            case NAVIGATION_BUTTON_RIGHT:
                TextView tv_right = findViewById(R.id.tv_right);
                tv_right.setTag(which);
                tv_right.setVisibility(View.VISIBLE);
                tv_right.setOnClickListener(this);
                break;
        }
    }
    @Override
    public void onClick(View v) {
        int which = ((Integer) v.getTag()).intValue();
        if (btnListener != null) {
            btnListener.OnNavigationButtonClick(which);
        }

    }

    public interface NavigationBarButtonListener {

        void OnNavigationButtonClick(int which);
    }

    public void setNavigationBarButtonListener(NavigationBarButtonListener listener) {
        btnListener = listener;
    }

}
